package fs.tileMap.data {
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author flyingSnow
	 */
	public class MapData {
		private var _width:int=1;
		private var _height:int = 1;
		private var _countOfData:int=1;
		protected var _dataArray:Array;
		private var _viewPort:Rectangle;		
		
		
		
		public function MapData(width:int,height:int,initValue:int=0) {
			_height = height;
			_width = width;
			
			_countOfData = _height * _width;
			
			initArray(initValue);
		}
		
		//{ region 初始化
		private function initArray(initValue:int):void {
			_dataArray = [];
			for (var i:int = 0; i < _countOfData; i++ ) {
				_dataArray.push(initValue);
			}
		}		
		//} endregion

		//{ region 公有函数	
		
		//} endregion
		
		//{ region 私有函数
		private function getScreenFromGlobal(gCl:int, gRw:int):Object {
			var scrPt:Object = {cl:0,rw:0};
			scrPt.cl = gCl - Math.floor(_viewPort.x);
			scrPt.rw = gRw - Math.floor(_viewPort.y);
			return scrPt;
		}
		private function getGlobalFromScreen(scrCl:int, scrRw:int):Object {
			var gPt:Object = {cl:0,rw:0};
			gPt.cl = scrCl + Math.floor(_viewPort.x);
			gPt.rw = scrRw + Math.floor(_viewPort.y);
			return gPt;
		}
		//} endregion
		
		//{ region 存取器		
		public function getScreenData(scrCl:int, scrRw:int):int {
			var gPt:Object = getGlobalFromScreen(scrCl, scrRw);
			return getValue(gPt.cl, gPt.rw);
		}
		public function setScreenData(scrCl:int,scrRw:int,value:int):void {
			var gPt:Object = getGlobalFromScreen(scrCl, scrRw);
			setValue(gPt.cl, gPt.rw,value);
		}
		public function setValue(cl:int, rw:int, value:int):void {
			_dataArray[cl* _height + rw]=value;
		}
		public function getValue(cl:int, rw:int):int {
			return _dataArray[cl * _height + rw];
		}
		
		public function get countOfData():int {
			return _countOfData;
		}
		
		public function get viewPort():Rectangle {
			return _viewPort.clone();
		}
		
		public function set viewPort(value:Rectangle):void {
			_viewPort = value;
			if (_viewPort.x < 0) {
				_viewPort.x = 0;
			}else if (_viewPort.right > _width) {
				_viewPort.x = _width - _viewPort.width;
			}
			
			if (_viewPort.y < 0) {
				_viewPort.y = 0;
			}else if (_viewPort.bottom > _height) {
				_viewPort.y = _height - _viewPort.height;
			}
		}
		
		public function get viewPortCl():Number {
			return _viewPort.x;
		}
		public function set viewPortCl(value:Number):void {

			_viewPort.x = value;
			if (_viewPort.x < 0) {
				_viewPort.x = 0;
			}else if (_viewPort.right > _width) {
				_viewPort.x = _width - _viewPort.width;
			}
		}
		
		public function get viewPortRw():Number {
			return _viewPort.y;
		}
		public function set viewPortRw(value:Number):void {
			_viewPort.y = value;
			if (_viewPort.y < 0) {
				_viewPort.y = 0;
			}else if (_viewPort.bottom > _height) {
				_viewPort.y = _height - _viewPort.height;
			}
		}
		
		public function get width():int {
			return _width;
		}
		
		public function get height():int {
			return _height;
		}
		//} endregion
	}

}